﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MessageSystem.Managed;
using MessageSystem.Managed.Interface;

namespace MessageSystem.Managed.Rpc
{
    public class RpcBroadCaster
    {
        private List<RpcProxy> _allProxy;
        private RpcHandler _handler;

        public RpcBroadCaster(RpcHandler handler)
        {
            _handler = handler;
        }

        public void Prepare(RpcProxy proxy)
        {
            if (null == _allProxy)
                _allProxy = new List<RpcProxy>();

            _allProxy.Add(proxy);
        }

        public void Prepare(List<RpcProxy> list)
        {
            if (null == _allProxy)
                _allProxy = new List<RpcProxy>();

            _allProxy.AddRange(list);
        }

        public void BroadCast(IMessage msg)
        {
            if (null != _allProxy)
            {
                RpcBroadcastMessage broadcast = new RpcBroadcastMessage();
                broadcast.WriteProxies = _allProxy;
                broadcast.WriteMessage = msg;
                _handler.Invoke(broadcast, 0, 0,0);
                _allProxy = null;
            }
        }
    }
}
